home *** CD-ROM | disk | FTP | other *** search
/ Celestin Apprentice 7 / Apprentice-Release7.iso / Source Code / C ++ / Frameworks / MacZoop 1.6.5 / More Classes / Window Classes / ZPictWindow.cpp < prev    next >
Encoding:
C/C++ Source or Header  |  1997-03-17  |  5.6 KB  |  268 lines  |  [TEXT/CWIE]

  1. /*************************************************************************************************
  2. *
  3. *
  4. *            ObjectMacZapp        -- a standard Mac OOP application template
  5. *
  6. *
  7. *
  8. *            ZPictWindow.cpp        -- a window that displays PICT files
  9. *
  10. *
  11. *
  12. *
  13. *
  14. *            © 1996, Graham Cox
  15. *
  16. *
  17. *
  18. *
  19. *************************************************************************************************/
  20.  
  21.  
  22. #include    "ZPictWindow.h"
  23. #include    "MacZoop.h"
  24. #include    "ZFile.h"
  25.  
  26. /*--------------------------------***  CONSTRUCTOR  ***---------------------------------*/
  27.  
  28. ZPictWindow::ZPictWindow( ZCommander* aBoss, const short windID )
  29.     : ZScroller( aBoss, windID, TRUE, TRUE )
  30. {
  31.     thePicture = NULL;
  32.     printable = TRUE;
  33. }
  34.  
  35.  
  36. /*---------------------------------***  DESTRUCTOR  ***---------------------------------*/
  37.  
  38. ZPictWindow::~ZPictWindow()
  39. {
  40.     if (thePicture)
  41.         KillPicture(thePicture);
  42. }
  43.  
  44. /*--------------------------------***  DRAWCONTENT  ***---------------------------------*/
  45. /*    
  46.  
  47. overrides ZScroller to draw the picture in the window
  48.  
  49. ----------------------------------------------------------------------------------------*/
  50.  
  51. void    ZPictWindow::DrawContent()
  52. {
  53.     Rect    pFrame;
  54.     
  55.     if (thePicture)
  56.     {
  57.         pFrame = (*thePicture)->picFrame;
  58.         OffsetRect(&pFrame, -pFrame.left, -pFrame.top);
  59.         DrawPicture(thePicture, &pFrame);
  60.     }
  61. }
  62.  
  63.  
  64. /*----------------------------------***  OPENFILE  ***----------------------------------*/
  65. /*    
  66.  
  67. overrides the base ZWindow to open a PICT file on disk to the window
  68.  
  69. ----------------------------------------------------------------------------------------*/
  70.  
  71. void    ZPictWindow::OpenFile( const OSType fType )
  72. {
  73.     // read the picture into the picture handle
  74.     
  75.     FInfo    fi;
  76.     short    refNum;
  77.     long    pSize;
  78.     Handle    temp = NULL;
  79.     Rect    pFrame;
  80.     
  81.     if (macFile.vRefNum != kNoFile)
  82.     {
  83.         FailOSErr(FSpGetFInfo(&macFile, &fi));    
  84.         
  85.         if (fi.fdType != 'PICT')
  86.             FailOSErr(paramErr);
  87.             
  88.         FailOSErr(FSpOpenDF(&macFile, fsCurPerm, &refNum ));
  89.         
  90.         // skip the first 512 bytes of the file
  91.         
  92.         try
  93.         {
  94.             FailOSErr(SetFPos(refNum, fsFromStart,  512));
  95.             
  96.             // how big is the rest of the file?
  97.             
  98.             FailOSErr(GetEOF(refNum, &pSize));
  99.             pSize -= 512;
  100.             
  101.             if (pSize > 0)
  102.             {
  103.                 FailNIL(temp = NewHandle(pSize));
  104.             
  105.                 HLock(temp);
  106.                 FailOSErr(FSRead(refNum, &pSize, *temp));
  107.                 HUnlock(temp);
  108.             }
  109.             
  110.             FSClose(refNum);
  111.             
  112.             if (thePicture)
  113.                 KillPicture(thePicture);
  114.                 
  115.             thePicture  = (PicHandle)temp;
  116.             
  117.             // set the scroll dimensions to the picture's frame
  118.             
  119.             pFrame = (*thePicture)->picFrame;
  120.             OffsetRect(&pFrame, -pFrame.left, -pFrame.top);
  121.             SetBounds( pFrame );
  122.             SetScrollAmount(8,8);
  123.             
  124.             pFrame.right += kStdScrollbarWidth;
  125.             pFrame.bottom += kStdScrollbarWidth;
  126.             
  127.             SetSize(pFrame.right, pFrame.bottom);
  128.         }
  129.         catch(OSErr err)
  130.         {
  131.             FSClose(refNum);
  132.             
  133.             if (temp)
  134.             {
  135.                 HUnlock(temp);
  136.                 DisposeHandle(temp);
  137.             }    
  138.             throw err;
  139.         }
  140.     }
  141.     inherited::OpenFile( fType );
  142. }
  143.  
  144.  
  145.  
  146. /*--------------------------------***  SAVEFILE  ***---------------------------------*/
  147. /*    
  148.  
  149. overrides the base ZWindow to save the image as a PICT file on disk
  150.  
  151. ----------------------------------------------------------------------------------------*/
  152.  
  153. void    ZPictWindow::SaveFile()
  154. {
  155.     // this saves the window contents as a PICT file in the file <macFile>.
  156.  
  157.     Handle        header = NULL;
  158.     ZFile*        theFile;
  159.     
  160.     if ((macFile.vRefNum != kNoFile) && (thePicture != NULL))
  161.     {
  162.         FailNIL( theFile = new ZFile( macFile ));
  163.         theFile->SetType( 'PICT' );
  164.         
  165.         if (! theFile->IsReal())
  166.             theFile->Create();
  167.         
  168.         // open the file for safe-saving
  169.             
  170.         theFile->OpenSafe();
  171.         
  172.         try
  173.         {
  174.             // make a header and write it out. This is just 512 bytes of zeroes.
  175.             
  176.             FailNIL( header = NewHandleClear( 512L ));
  177.             
  178.             theFile->Write( header );
  179.             DisposeHandle(header);
  180.             header = NULL;
  181.             
  182.             // now write the picture handle
  183.             
  184.             theFile->Write((Handle) thePicture );
  185.             theFile->Close();    
  186.         }
  187.         catch( OSErr err )
  188.         {    
  189.             theFile->Close();
  190.             ForgetObject( theFile );
  191.             
  192.             if (header)
  193.                 DisposeHandle(header);
  194.             
  195.             throw err;
  196.         }
  197.         
  198.         ForgetObject( theFile );
  199.         inherited::SaveFile();
  200.     }
  201. }
  202.  
  203. /*--------------------------------***  UPDATEMENUS  ***---------------------------------*/
  204. /*
  205. Enable the Copy command if we have a picture    
  206. ----------------------------------------------------------------------------------------*/
  207.  
  208. void    ZPictWindow::UpdateMenus()
  209. {
  210.     if ( thePicture )
  211.         gMenuBar->EnableCommand( kCmdCopy );
  212.  
  213.     inherited::UpdateMenus();
  214. }
  215.  
  216.  
  217. /*--------------------------------***  CANPASTETYPE  ***--------------------------------*/
  218. /*    
  219. we can accept Paste commands of type PICT
  220. ----------------------------------------------------------------------------------------*/
  221.  
  222. Boolean    ZPictWindow::CanPasteType()
  223. {
  224.     return gClipboard->QueryType( 'PICT' );
  225. }
  226.  
  227.  
  228. /*-----------------------------------***  DOCOPY  ***-----------------------------------*/
  229. /*
  230. copy the image to the clipboard    
  231. ----------------------------------------------------------------------------------------*/
  232.  
  233. void    ZPictWindow::DoCopy()
  234. {
  235.     if ( thePicture )
  236.         gClipboard->PutData( thePicture );
  237. }
  238.  
  239. /*----------------------------------***  DOPASTE  ***-----------------------------------*/
  240. /*
  241. copy a PICT from the clipboard to our local picture    
  242. ----------------------------------------------------------------------------------------*/
  243.  
  244. void    ZPictWindow::DoPaste()
  245. {
  246.     Rect        pFrame;
  247.     PicHandle    p = (PicHandle) gClipboard->GetData( 'PICT' );
  248.     
  249.     FailNIL( p );
  250.     
  251.     if ( thePicture )
  252.         KillPicture( thePicture );
  253.         
  254.     thePicture = p;
  255.     
  256.     pFrame = (*thePicture)->picFrame;
  257.     OffsetRect( &pFrame, -pFrame.left, -pFrame.top );
  258.     SetBounds( pFrame );
  259.     SetScrollAmount( 8, 8 );
  260.     
  261.     pFrame.right += kStdScrollbarWidth;
  262.     pFrame.bottom += kStdScrollbarWidth;
  263.     
  264.     SetSize( pFrame.right, pFrame.bottom );
  265.     
  266.     dirty = TRUE;
  267. }
  268.